// const { data } = require("jquery")

$(()=>{
    // 发起请求获取文章类别数据
    function aa(){
        $.ajax({
            type:"get",
            url:"admin/category/list",
            headers:{
                Authorization:localStorage.getItem(`token`)
            },
            success:function(res){
                // 判断code是否等于200，是的话就把数据渲染到页面上，否则返回到登陆页面
                if(res.code==200){
                    let htmlstr=template(`cate_item`,res)
                    $(`tbody`).html(htmlstr)
                }else{
                    window.location.href="./login.html"
                }
            }
        })
    }
    aa()
    //点击新增：
  
    $(`#xinzengfenlei`).on(`click`,function(e){
        console.log(1)
        e.stopPropagation()
        $('#myModal').modal()
    })
   
    $(`.ssh`).on(`click`,function(e){
        console.log(2)
        e.stopPropagation()
        e.preventDefault()
        let name=$(`[name=name]`).val().trim()
        let slug=$(`[name=slug]`).val().trim()
        $.ajax({
            type:"post",
            url:"admin/category/add",
            data:{name,slug},
            headers:{
                // Authorization:localStorage.getItem(`token`)
            },
            success:function(res){
                // console.log(res)
                if(res.code==200){
                    layer.msg(`创建成功`)
                    $('[name="name"]').val('')
                    $('[name="slug"]').val('')
                    $('#myModal').modal('hide')
                    aa()
                }else{
                    layer.msg(res.msg)
                }
            }
        })
    })

    $(`tbody`).on(`click`,`a.btn-danger`,function(){
        console.log(1)
       let id= $(this).attr(`data-id`)
       console.log(id)
       if(confirm(`确定要删除？`)){
        $.ajax({
            type:"post",
            url:"admin/category/delete",
            data:{id},
            headers:{
                // Authorization:localStorage.getItem(`token`)
            },
            success:function(back){
                layer.msg(back.msg)
            }
        })
        aa()
      }
    })

 
    // 点击编辑
    let id
    let name
    let slug
    $(`tbody`).on(`click`,`.btn-edit`,function(e){
        console.log(1)
        e.preventDefault()
        $('#editModal').modal()
         id=$(this).attr(`data-id`)-0
         name=$(this).parent().prev().prev().text().trim()
         slug=$(this).parent().prev().text().trim()
        $('#name').val(name)
        $('#slug').val(slug)
       
    })
    $(`.btn-two`).on(`click`,function(e){
        console.log(2)
        e.preventDefault()
        name=$('#name').val()
        slug=$(`#slug`).val()
        console.log({id,name,slug})
        $.ajax({
            type:"post",
            url:"admin/category/edit",
            data:{id,name,slug},
            success:function(res){
                // console.log(res)
                if(res.code==200){
                    layer.msg(`保存成功`)
                    $('#editModal').modal('hide')
                    aa()
                    
                }else{
                  
                    layer.msg(res.msg)
                }
            }
        })
    })
})